package com.linjj.seckill.controller;

import com.linjj.seckill.access.AccessLimit;
import com.linjj.seckill.domain.User;
import com.linjj.seckill.vo.RegistVo;
import com.linjj.seckill.vo.Result;
import com.linjj.seckill.service.IUserService;
import com.linjj.seckill.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 登录控制器
 * */
@Controller
public class LoginController {
    @Autowired
    private IUserService userService;

    /**
     * 登录
     * */
    @AccessLimit(seconds=5,maxCount=5,needLogin=false)
    @ResponseBody
    @PostMapping("/do_login")
    public  Result do_login(HttpServletResponse response,@Valid LoginVo loginVo){
        userService.login(response,loginVo);
        return Result.success(true);
    }
    /**
     * 注册
     * */
    @AccessLimit(seconds=5,maxCount=5,needLogin=false)
    @ResponseBody
    @PostMapping("/do_regist")
    public  Result do_regist(HttpServletResponse response,@Valid RegistVo registVo){
        userService.register(registVo);
        System.out.println(registVo);
        return Result.success(true);
    }
    @AccessLimit(seconds=5,maxCount=5,needLogin=false)
    @ResponseBody
    @GetMapping("/loginInfo")
    public Result getLoginInfo(User user){
        return Result.success(user);
    }

    @AccessLimit(seconds=5,maxCount=5,needLogin=false)
    @ResponseBody
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        userService.logout(request);
        return Result.success(true);
    }
}
